//
//  OELoginHomeViewController.m
//  OneEightyNine
//
//  Created by apple on 2019/3/1.
//  Copyright © 2019 stoneobs. All rights reserved.
//

#import "OELoginHomeViewController.h"
#import "OERegisterFirstViewController.h"
#import "OEFindPwdFirstViewController.h"
@interface OELoginHomeViewController ()
@property(nonatomic, strong) UITextField                     *phoneTextFiled;/**<  */
@property(nonatomic, strong) UITextField                     *pwdTextFiled;/**<  */
@property(nonatomic, strong) STButton                     *loginButton;/**<  */
@end

@implementation OELoginHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configSubView];
    [self st_addAdjustView:self.loginButton offset:10];
    // Do any additional setup after loading the view.
}

#pragma mark --configSubView
- (void)configSubView{
    [self vhl_setNavBarBackgroundAlpha:0];
    
    [self st_setLeftItemWithTitle:OESTRING(@"CANCEL") titleColor:FirstTextColor];
    
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 200)];

    
    UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"180×132LOGO"]];
    imageView.right = UIScreenWidth  -15;
    imageView.top = 20;
    [header addSubview:imageView];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, imageView.centerY, imageView.left - 30, 100)
                                                     text:OESTRING(@"HI!\nWELCOME TO LOG IN")
                                                textColor:FirstTextColor
                                                     font:18
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:titleLable];
    

    
     self.phoneTextFiled =  [self textFiledWithLeftTitle:[NSString stringWithFormat:@"%@ >",OE_PHONE_PREFIX]
                                            placeHolader:OESTRING(@"Phone number") rightView:nil];
    self.phoneTextFiled.top = titleLable.bottom + 40;
    [header addSubview:self.phoneTextFiled];
    
    self.pwdTextFiled =  [self textFiledWithLeftTitle:@"" placeHolader:OESTRING(@"Password") rightView:nil];
    self.pwdTextFiled.top = self.phoneTextFiled.bottom ;
    self.pwdTextFiled.secureTextEntry = YES;
    self.pwdTextFiled.text = STUserManger.defult.pwd;
    self.phoneTextFiled.text = STUserManger.defult.account;
    [header addSubview:self.pwdTextFiled];
    __weak typeof(self) weakSelf =  self;
    STButton * forgetbutton = [[STButton alloc] initWithFrame:CGRectMake(15, self.pwdTextFiled.bottom, 200, 44)
                                                      title:OESTRING(@"Forget the password")
                                                 titleColor:SecendTextColor
                                                  titleFont:14
                                               cornerRadius:0
                                            backgroundColor:nil
                                            backgroundImage:nil
                                                      image:nil];
    [forgetbutton setClicAction:^(UIButton *sender) {
        [weakSelf.navigationController pushViewController:OEFindPwdFirstViewController.new animated:YES];
    }];
    [header addSubview:forgetbutton];
    
    
    STButton * registerbutton = [[STButton alloc] initWithFrame:CGRectMake(15, self.pwdTextFiled.bottom, 200, 44)
                                                        title:OESTRING(@"Sign up")
                                                   titleColor:SecendTextColor
                                                    titleFont:14
                                                 cornerRadius:0
                                              backgroundColor:nil
                                              backgroundImage:nil
                                                        image:nil];
    [registerbutton setClicAction:^(UIButton *sender) {
        [weakSelf.navigationController pushViewController:OERegisterFirstViewController.new animated:YES];
    }];
    registerbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    registerbutton.right = UIScreenWidth - 15;
    [header addSubview:registerbutton];
    

    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(5, registerbutton.bottom + 20, UIScreenWidth - 30, 44)
                                                  title:OESTRING(@"SIGN IN")
                                             titleColor:UIColor.whiteColor
                                              titleFont:15
                                           cornerRadius:22
                                        backgroundColor:OE_LIMITE_COLOR
                                        backgroundImage:nil
                                                  image:nil];
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    confimButton.centerX = self.tableView.width / 2;
    [header addSubview:confimButton];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf sendLoginRequest];
    }];
    self.loginButton = confimButton;
    STButton * emailButton = [self deflutButtonWithImage:@"70×70邮箱" handle:^{
        
    }];
    emailButton.centerX = UIScreenWidth / 2;
    emailButton.top = self.loginButton.bottom + 100;
    [header addSubview:emailButton];
    
    STButton * fButton = [self deflutButtonWithImage:@"70×70facebook" handle:^{
        
    }];
    fButton.right = emailButton.left - 40 ;
    fButton.top = self.loginButton.bottom + 100;
    [header addSubview:fButton];
    
    STButton * rButton = [self deflutButtonWithImage:@"70×70Instagram" handle:^{
        
    }];
    rButton.left = emailButton.right + 40 ;
    rButton.top = self.loginButton.bottom + 100;
    [header addSubview:rButton];
    
    header.height = rButton.bottom;
    self.tableView.tableHeaderView = header;
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.showsVerticalScrollIndicator = NO;
}
- (STButton*)deflutButtonWithImage:(NSString*)imageName handle:(void(^)(void))handle{
    STButton * button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)
                                                  title:nil
                                             titleColor:nil
                                              titleFont:0
                                           cornerRadius:0
                                        backgroundColor:nil
                                        backgroundImage:nil
                                                  image:[UIImage imageNamed:imageName]];
    [button setClicAction:^(UIButton *sender) {
        if (handle) {
            handle();
        }
    }];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    return button;
    
}
- (UITextField*)textFiledWithLeftTitle:(NSString*)leftTitle placeHolader:(NSString*)placeHolader  rightView:(UIView*)rightView{
    UITextField * textFiled = [[UITextField alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth-30, 50)];
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, textFiled.height, textFiled.height)
                                                     title:leftTitle
                                                titleColor:OE_LIMITE_COLOR
                                                 titleFont:14
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    if (leftTitle.length) {
        textFiled.leftView = buyButton;
    }
    
    textFiled.leftViewMode = UITextFieldViewModeAlways;
    buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    textFiled.placeholder = placeHolader;
    
    textFiled.rightView = rightView;
    textFiled.rightViewMode = UITextFieldViewModeAlways;
    [textFiled st_showBottomLine];
    return textFiled;
}
#pragma mark --NetWork Method
- (void)sendLoginRequest{
    if (self.phoneTextFiled.text.length != 11) {
        [SVProgressHUD showErrorWithStatus:OESTRING(@"Please input the correct mobile phone number")];
        return;
    }
    
    if (!self.pwdTextFiled.text.length ) {
        [SVProgressHUD showErrorWithStatus:OESTRING(@"Please enter your password")];
        return;
    }
    [OERegisterDataController  sendLoginRequestWithMobile:self.phoneTextFiled.text password:self.pwdTextFiled.text handle:^(BOOL success, NSString *errorString, NSDictionary *resp) {
       
        if (success) {
            [STUserManger.defult updateAccount:self.phoneTextFiled.text pwd:self.pwdTextFiled.text];
            [SVProgressHUD showSuccessWithStatus:@"Login successful"];
            OEUserModel * user = [OEUserModel mj_objectWithKeyValues:resp];
            [STUserManger.defult updateUserModel:user];
            
            UIWindow * window =  UIApplication.sharedApplication.delegate.window ;
            window.rootViewController = TMTabbarViewController.new;
            [window st_showAnimationWithType:STAnimationTypecube];
        }
    }];
}
@end
